//
//  ConNewEditCardVc.m
//  Shengzhuli
//
//  Created by FXGuo on 2018/1/19.
//  Copyright © 2018年 kamangkeji. All rights reserved.
//

#import "ConNewEditCardVc.h"
#import "NewEditCardCell.h"
#import "UIActionSheet+Extension.h"
#import "PersonNetWork.h"
#import "MBProgressHUD+MJ.h"
#import "SZLAccountHelper.h"

@interface ConNewEditCardVc ()<UITableViewDataSource,UITableViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIActionSheetDelegate>
@property(nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) UIImage *avatorImage;
@property (nonatomic,weak) UIButton *avaBtn;
@property (nonatomic,strong) PersonInfo *editInfo;

@end

@implementation ConNewEditCardVc

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"编辑名片";
    [self setNavi];
    self.editInfo = [self.info copy];
    [self.view addSubview:self.tableView];
}
- (void)setNavi
{
    UIButton *saveBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 30)];
    [saveBtn setTitle:@"保存" forState:UIControlStateNormal];
    [saveBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    saveBtn.titleLabel.font = Font(16);
    [saveBtn addTarget:self action:@selector(saveClick) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:saveBtn];
}
- (void)saveClick
{
    if (self.editInfo.name.length == 0) {
        [MBProgressHUD showOnlyText:@"请输入姓名" view:self.view];
        return;
    }
    if (self.editInfo.position.length == 0) {
        [MBProgressHUD showOnlyText:@"请输入职务" view:self.view];
        return;
    }
    if (self.editInfo.detailedAddress.length == 0) {
        [MBProgressHUD showOnlyText:@"请输入地址" view:self.view];
        return;
    }
    if (self.editInfo.personalizedSignature.length == 0) {
        [MBProgressHUD showOnlyText:@"请输入座右铭" view:self.view];
        return;
    }
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"token"] = [SZLAccountHelper shareInstance].token;
    parame[@"portraitUrl"] = self.editInfo.portraitUrl;
    parame[@"name"] = self.editInfo.name;
    parame[@"mobilePhone"] = self.editInfo.mobilePhone;
    parame[@"position"] = self.editInfo.position;
    parame[@"personalizedSignature"] = self.editInfo.personalizedSignature;
    parame[@"detailedAddress"] = self.editInfo.detailedAddress;
    [self showWaiter];
    [PersonNetWork editCardInfoWithParame:parame response:^(BOOL success, NSString *msg) {
        [self hideWaiter];
        if (success) {
            [MBProgressHUD showOnlyText:@"保存成功" view:self.view];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                if (self.isFromCard) {
                    [self.navigationController popToRootViewControllerAnimated:YES];
                }else {
                    [self.navigationController popViewControllerAnimated:YES];
                }
            });
        }else {
            [MBProgressHUD showError:msg];
        }
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewEditCardCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([NewEditCardCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.info = self.info;
    cell.editInfo = self.editInfo;
    cell.avaClickBlock = ^(UIButton *btn) {
        self.avaBtn = btn;
        [self takeAvator];
    };
    return cell;
}
//拍照//相册
- (void)takeAvator
{
    UIActionSheet *action = [[UIActionSheet alloc]initWithTitle:@"选择图片" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"从相册选择",nil];
    [action showInView:self.view];
    [action handlerClickedButton:^(UIActionSheet *actionSheet, NSInteger btnIndex) {
        if (btnIndex == 0) {
            BOOL isCameraSupport = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
            if (!isCameraSupport) {
                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"相机不可用,请检查相机" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alert show];
            }else{
                UIImagePickerController *cameraPicker = [[UIImagePickerController alloc]init];
                cameraPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
                cameraPicker.allowsEditing = YES;
                [self presentViewController:cameraPicker animated:YES completion:nil];
                cameraPicker.delegate = self;
            }
        }else if (btnIndex == 1){
            UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
            imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            imagePicker.allowsEditing = YES;
            imagePicker.delegate = self;
            [self presentViewController:imagePicker animated:YES completion:nil];
        }
    }];
}
#pragma mark ---- UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    self.avatorImage = info[@"UIImagePickerControllerEditedImage"];
    [self.avaBtn setImage:self.avatorImage forState:UIControlStateNormal];
    //上传头像
    [self upLoadFile:self.avatorImage];
    [picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)upLoadFile:(UIImage *)image
{
    [self showClearBgWaiter];
    [PersonNetWork upLoadImageWithParame:@{@"optionType":@"3"} image:image response:^(BOOL success, NSString *msg,NSString *path) {
        [self hideWaiter];
        self.editInfo.portraitUrl = path;
    }];
}
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH-SafeAreaTopHeight) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.tableFooterView = [[UIView alloc]init];
        _tableView.rowHeight = 560;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([NewEditCardCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([NewEditCardCell class])];
    }
    return _tableView;
}

@end
